package com.czkj.hwz.decorate.control;
import android.content.Context;
import android.location.Location;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.czkj.hwz.decorate.IMP.HttpDoImp;
import com.czkj.hwz.decorate.base.HttpUrl;
import com.czkj.hwz.decorate.bean.Enterprise;
import com.czkj.hwz.decorate.bean.Goods;


import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * Created by Administrator on 2017/10/5.
 */

public class IndexGet {
    private Context mContext;
    private HttpDoImp mHttpDoImp;
    public IndexGet(Context mContext) {
        this.mContext=mContext;
        this.mHttpDoImp = (HttpDoImp)mContext;
    }
    public IndexGet(Context mContext,HttpDoImp httpDoImp){
        this.mContext = mContext;
        this.mHttpDoImp = httpDoImp;
    }

    public void getIndex(Location location) {
        RequestParams params = new RequestParams(HttpUrl.INDEX_GET);
//        params.addQueryStringParameter("longitude",  String.valueOf(location.getLatitude()));
//        params.addQueryStringParameter("latitude", String.valueOf(location.getLongitude()));
        params.addQueryStringParameter("longitude", "119.038657");
        params.addQueryStringParameter("latitude", "33.609279");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                JSONObject jsonReult = JSON.parseObject(result);
                JSONObject areaInfo = null;
                try {
                    areaInfo = jsonReult.getJSONObject("AreaInfo");
                }catch (Exception e) {
                    areaInfo = null;
                }
                String[] imgUrl = null;
                try {
                    String banner = jsonReult.getString("Banner");
                    banner = banner.replace("[", "").replace("]", "").replace("\"", "");
                    imgUrl = banner.split(",");

                }catch (Exception e) {
                    imgUrl = null;
                }
                List<Enterprise> enterprises = null;
                try {
                    enterprises = JSON.parseArray(jsonReult.getString("Enterprise"),Enterprise.class);
                }catch (Exception e) {

                }
                List<Goods> goodses = null;
                try {
                    goodses = JSON.parseArray(jsonReult.getString("Goods"),Goods.class);
                }catch (Exception e) {

                }
                mHttpDoImp.onSuccess("indexGet",areaInfo,imgUrl,enterprises,goodses);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                mHttpDoImp.onError(ex.getMessage());
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });
    }
}
